import axios from "axios";
import {instance} from "./request";
/**
 * 动态加载百度地图api函数
 * @param {String} ak  百度地图AK，必传
 */
const ak = 'F0QZjfxl5SM6vG3XwdoM5UVVjGWwPM0K'
export default function loadBMap(ak,version) {
  return new Promise(function(resolve, reject) {
    if (typeof window.BMap !== 'undefined') {
      resolve(window.BMap)
      return true
    }
    window.onBMapCallback = function() {
      resolve(window.BMap)
    }
    let script = document.createElement('script')
    script.type = 'text/javascript'
    
    script.src =
      'http://api.map.baidu.com/api?v=3.0&ak=' + ak + '&callback=onBMapCallback'
    script.onerror = reject
    document.head.appendChild(script)
  })
}
/**
 * 
 * @param {*} ak 
 * @returns 
 */
export  function getQueryResults(query,region='江西') {
  let tail = '$city_limit=true&output=json&ak='+ak;
  query = encodeURIComponent(query)
  region = encodeURIComponent(region)

  let url = `https://api.map.baidu.com/place/v2/suggestion?query=${query}&region=${region}&city_limit=true&output=json&ak=${ak}`
  return axios.get(url,{
    dataType:'JSONP'
  })
}
